package DataBase

import (
	"PaySalary/Model"
)

type PayrollDatabase struct {
	itsEmployees map[int]*Model.Employee
	itsUnionMembers map[int]int
}

var theInstance = &PayrollDatabase{map[int]*Model.Employee{}, map[int]int{}}

func (pd* PayrollDatabase) clear() { //清除函数
	pd.itsEmployees = map[int]*Model.Employee{}
	pd.itsUnionMembers = map[int]int{}
}

func (pd* PayrollDatabase)GetEmployee(empid int)*Model.Employee {
	return pd.itsEmployees[empid]
}

func (pd* PayrollDatabase)AddEmployee(empid int,e* Model.Employee) {
	pd.itsEmployees[empid] = e
}

func (pd* PayrollDatabase) DeleteEmployee(empid int) {
	delete(pd.itsEmployees, empid)
}

func (pd* PayrollDatabase) AddUnionMember(memberid int, e* Model.Employee)  {
	empid := e.GetEmpid()
	pd.itsUnionMembers[memberid] = empid
}

func (pd* PayrollDatabase) GetUnionMember(memberId int) *Model.Employee{
	if empid, ok := pd.itsUnionMembers[memberId]; ok {
		return pd.itsEmployees[empid]
	}
	return nil
}

func (pd* PayrollDatabase) RemoveUnionMember(memberID int) {
	delete(pd.itsUnionMembers, memberID)
}

func (pd* PayrollDatabase) GetAllEmployeeIds() []int {
	var list []int

	for key := range pd.itsEmployees {
		list = append(list, key)
	}
	return list
}

